﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Linq;
using WJ.Infrastructure.DDD;


namespace WJ.Infrastructure.NH
{ 
    public class NHContext : IUnitOfWork
    {

        public NHContext()
        {

        }

        public void CommitAndRefreshChanges()
        {
            throw new System.NotImplementedException();
        }

        public void RollbackChanges()
        {
            GlobalOperate.RollbackChanges();
        }

        public void BeginChanges()
        {
            GlobalOperate.BeginChanges();
        }

        public int Commit()
        {

                try
                {

                    GlobalOperate.CommitChanges();

                    return 1;
                }
                catch (Exception e)
                {
                    RollbackChanges();

                    return 0;
                }
          
        }



        public static IQueryable<Tentity> GetIQuery<Tentity>() where Tentity : class, IEntity, new()
        {
            return new NhQueryable<Tentity>((ISessionImplementor)NHibernateSessionManager.Instance.GetSession());
        }

        public static ISession GetSession()
        {
            return NHibernateSessionManager.Instance.GetSession();
        }

    }
}